package com.yuexin.project.dailymenu.dishlist.controller;

import com.yuexin.common.utils.ServletUtils;
import com.yuexin.common.utils.StringUtils;
import com.yuexin.common.utils.file.FileUploadUtils;
import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.dailymenu.dishlist.domain.DishList;
import com.yuexin.project.dailymenu.dishlist.service.DishIListService;
import com.yuexin.project.dailymenu.dishtype.service.ITypeService;
import com.yuexin.project.dailymenu.everyday.domain.EverydayMenu;
import com.yuexin.project.dailymenu.menumanager.domain.Manager;
import com.yuexin.project.order.domain.MealsSetting;
import com.yuexin.project.order.service.ISettingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 菜品列 信息操作处理
 * 
 * @author yuexin
 * @date 2018-10-10
 */
@Controller
@RequestMapping("/dailymenu/dishlist")
public class DishListController extends BaseController
{
    private String prefix = "dailymenu/dishlist";
	
	@Autowired
	private DishIListService listService;
	
	@Autowired
	private ITypeService typeService;

	@Autowired
	private ISettingService settingService;
	
	@RequiresPermissions("dailymenu:dishlist:view")
	@GetMapping()
	public String list()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询菜品列列表
	 */
	@RequiresPermissions("dailymenu:dishlist:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DishList dishList)
	{
		startPage();
		String dishName = ServletUtils.getParameter("searchValue");
		dishList.setDishName(dishName);
		dishList.setCompanyId(ShiroUtils.getCompanyId());
        List<DishList> list = listService.selectListList(dishList);
		return getDataTable(list);
	}

	/**
	 * 判断是否需要设置售价
	 * @param companyId
	 */
	private Boolean isPrice(String companyId){
		MealsSetting setting = settingService.selectSettingByCompanyId(companyId);
		boolean result = "1".equals(setting.getBreakfastType()) && "-1".equals(setting.getBreakfastType1()) ? true : false;
		result = "1".equals(setting.getDinnerType()) && "-1".equals(setting.getDinnerType1()) ? true : result;
		result = "1".equals(setting.getLunchType()) && "-1".equals(setting.getLunchType1()) ? true : result;
		result = "1".equals(setting.getOtherType()) && "-1".equals(setting.getOtherType1()) ? true : result;
		return result;
	}

	/**
	 * 新增菜品列
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		String companyId = ShiroUtils.getCompanyId();
		mmap.put("dishTypes", typeService.selectDishTypeAll(companyId));
		mmap.put("isPrice", isPrice(companyId));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存菜品列
	 */
	@RequiresPermissions("dailymenu:dishlist:add")
	@Log(title = "菜品列", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DishList list)
	{		
		return toAjax(listService.insertList(list));
	}

	/**
	 * 修改菜品列
	 */
	@GetMapping("/edit/{dishId}")
	public String edit(@PathVariable("dishId") Integer dishId, ModelMap mmap)
	{
		String companyId = ShiroUtils.getCompanyId();
		mmap.put("list", listService.selectListById(dishId));
		mmap.put("dishTypes", typeService.selectDishTypeAll(companyId));
		mmap.put("isPrice", isPrice(companyId));
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存菜品列
	 */
	@RequiresPermissions("dailymenu:dishlist:edit")
	@Log(title = "菜品列表", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DishList list)
	{		
		return toAjax(listService.updateList(list));
	}
	
	/**
	 * 删除菜品列
	 */
	@RequiresPermissions("dailymenu:dishlist:remove")
	@Log(title = "菜品列", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(listService.deleteListByIds(ids));
	}
	
	/**
	 * 导出
	 */
	@Log(title = "列表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("dailymenu:dishlist:export")
    @PostMapping("/exportdistlist")
    @ResponseBody
    public AjaxResult export(DishList type)
    {
        List<DishList> list = listService.selectListList(type);
        ExcelUtil<DishList> util = new ExcelUtil<DishList>(DishList.class);
        return util.exportExcel(list, "dishlist");
    }
	
	/**
     * 加载菜品列表树
     */
    @GetMapping("/menuDishTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuDishTreeData(Manager manager)
    {

    	manager.setCompanyId(ShiroUtils.getCompanyId());
        List<Map<String, Object>> tree = listService.menuDishTreeData(manager);
        return tree;
    }
    
    /**
     * 加载每日菜单菜品列表树
     */
    @GetMapping("/dailyMenuDishTreeData")
    @ResponseBody
    public List<Map<String, Object>> dailyMenuDishTreeData(EverydayMenu everydayMenu)
    {
    	everydayMenu.setCompanyId(ShiroUtils.getCompanyId());
        List<Map<String, Object>> tree = listService.dailyMenuDishTreeData(everydayMenu);
        return tree;
    }
    
    /**
     * 上传菜品图片
     */
    @GetMapping("/dishImage/{dishId}")
    public String avatar(@PathVariable("dishId") Integer dishId,ModelMap mmap)
    {
    	mmap.put("dishs", listService.selectListById(dishId));
        return prefix + "/dishImage";
    }
    
    /**
     * 保存菜品图片
     */
    @PostMapping("/updateDishImage")
    @ResponseBody
    public AjaxResult updateDishImage(DishList dish,@RequestParam("dishId") Integer dishId , @RequestParam("avatarfile") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
            	//System.out.println("path========="+YueXinConfig.getAvatarPath());
            	String filepath = "";
            	String osName = System.getProperties().getProperty("os.name");
                if(osName.equals("Linux"))
                {
                	filepath = "/profile/avatar/";
                }
                else
                {
                	filepath = "D:/profile/avatar/";
                }
                //String avatar = FileUploadUtils.upload(YueXinConfig.getAvatarPath(), file);
                String avatar = FileUploadUtils.upload(filepath, file);
                //System.out.println("dishId========="+dishId);
                dish.setDishPic(avatar);
                dish.setDishId(dishId);
                listService.updateList(dish);
               /* if (userService.updateUserInfo(user) > 0)
                {
                    setUser(userService.selectUserById(user.getUserId()));
                    return success();
                }*/
                
                return success();
            }
            return error();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
